package com.nl.controller.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsRule;
import com.nl.controller.web.mapper.SmsRuleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SmsRoleService {
    @Autowired
    private SmsRuleMapper smsRuleMapper;

    @Autowired
    private CommonService commonService;

    @SystemServiceLog(description = "查询角色信息表", value = false)
    public PageInfo<SmsRule> findRoel(int page, int limit) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, limit);
        List<SmsRule> list = new ArrayList<>();
        try {
            list = smsRuleMapper.findRule(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询角色信息出现异常");
            throw new SmsException("查询短信发送明细出现异常");
        }
        PageInfo<SmsRule> pageInfo = new PageInfo<SmsRule>(list);
        return pageInfo;
    }

    @SystemServiceLog(description = "删除角色信息", value = false)
    public int removeRole(String roleId) {
        int result;
        try {
            smsRuleMapper.removeRole(roleId);
            commonService.createSmsOperLog("5", "删除角色" + roleId, "0", "角色管理");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除角色信息出现异常：" + roleId);
            commonService.createSmsOperLog("5", "删除角色" + roleId, "1", "角色管理");
            throw new SmsException("删除角色信息：" + roleId);
        }
        try {
            result = smsRuleMapper.removeFunctionIds(roleId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除角色信息对应的权限出现异常：" + roleId);
            throw new SmsException("删除角色权限信息：" + roleId);
        }
        try {
            smsRuleMapper.removeUserRole(roleId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除用户对应的角色出现异常：" + roleId);
            throw new SmsException("删除用户的角色：" + roleId);
        }
        return result;
    }

    @SystemServiceLog(description = "添加角色信息", value = false)
    public int addRole(String roleName, String functionIds, String duty, String msisdn) {
        String roleId;
        try {
            roleId = smsRuleMapper.selectMaxId();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询最大角色号");
            throw new SmsException("查询最大角色号");
        }

        SmsRule smsRule = new SmsRule();
        smsRule.setRoleId(roleId);
        smsRule.setRoleName(roleName);
        smsRule.setDuty(duty);
        smsRule.setCreateOperator(msisdn);
        smsRule.setModifyOperator(msisdn);
        //添加权限
        int j = this.addFunctions(roleId, functionIds);
        int result;
        try {
            result = smsRuleMapper.addRole(smsRule);
            commonService.createSmsOperLog("2","添加角色的信息"+roleName,"0","角色管理");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("2","添加角色的信息"+roleName,"1","角色管理");
            log.error("添加角色信息出现异常：" + roleId);
            throw new SmsException("添加角色信息：" + roleId);
        }
        return result;
    }

    @SystemServiceLog(description = "添加角色权限", value = false)
    public int addFunctions(String roleId, String functionIds) {
        String function[] = functionIds.split(",");
        List<String> functionId = new ArrayList<String>();
        //添加父节点
        for (int i = 0; i < function.length; i++) {
            functionId.add(function[i]);
            functionId.add(function[i].substring(0, 1) + "0");
        }
        //去重
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < functionId.size(); i++) {
            if (!list.contains(functionId.get(i))) {
                list.add(functionId.get(i));
            }
        }
        //循环添加权限
        int j = 0;
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("functionId", list.get(i));
            try {
                smsRuleMapper.addFunction(map);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("添加角色权限出现异常：" + roleId);
                throw new SmsException("添加角色权限信息：" + roleId);
            }
            j++;
        }
        log.info("添加权限:{}", j);
        return j;
    }

    @SystemServiceLog(description = "根据角色编号查询角色信息", value = false)
    public SmsRule findRoleBYRoleId(String roleId) {
        SmsRule smsRule = new SmsRule();
        List<SmsRule> list = new ArrayList<>();
        try {
            smsRule = smsRuleMapper.findRoleByRoleId(roleId);
            list = smsRuleMapper.findFunctionByRoleId(roleId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询角色信息：" + roleId);
            throw new SmsException("查询角色信息：" + roleId);
        }

        //添加权限属性
        if (list.size() > 0) {
            StringBuffer sbf = new StringBuffer();
            for (int i = 0; i < list.size() - 1; i++) {
                sbf.append(list.get(i).getFunctionId() + ",");
            }
            sbf.append(list.get(list.size() - 1).getFunctionId());
            smsRule.setFunctionId(sbf.toString());
        }
        return smsRule;
    }


    @SystemServiceLog(description = "修改角色信息", value = false)
    public int modifyRole(String roleId, String roleName, String functionIds, String duty, String msisdn) {
        SmsRule smsRule = new SmsRule();
        smsRule.setRoleId(roleId);
        smsRule.setRoleName(roleName);
        smsRule.setDuty(duty);
        smsRule.setModifyOperator(msisdn);
        int result;
        try {
            smsRuleMapper.modifyRole(smsRule);
            smsRuleMapper.removeFunctionIds(roleId);
            result = this.addFunctions(roleId, functionIds);
            commonService.createSmsOperLog("3","修改角色的信息"+roleId,"0","角色管理");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改角色信息：" + roleId);
            commonService.createSmsOperLog("3","修改角色的信息"+roleId,"1","角色管理");
            throw new SmsException("修改角色信息：" + roleId);
        }
        return result;
    }
}